from Ledger import Ledger


def write_to_file(ledger_list, mod):
    file_name = "ledger.txt"
    with open(file_name, mod, encoding='utf-8') as file:
        for item in ledger_list:
            file.write(str(item))

def get_leger_list_from_file(file_name):
    ledger_list = []
    with open(file_name, 'r', encoding='utf-8') as file:
        for line in file:  # 逐行读取文件
            line = line.strip().split()  # 去掉首尾空白并分割行
            if len(line) == 7:  # 确保每行有7个字段
                ledger = Ledger(line[0], line[1], line[2], line[3], line[4], line[5], line[6])
                ledger_list.append(ledger)
            else:
                print(f"跳过无效行：{line}")
    return ledger_list
